package com.youzikj.wx.pay;

import java.io.Serializable;
import java.util.HashMap;
import java.util.UUID;

import com.youzikj.wx.comm.util.CommonUtil;
import com.youzikj.wx.pay.hongbao.MD5Util;
import com.youzikj.wx.pay.hongbao.MoneyUtils;

public class WxParams implements Serializable{

	private static final long serialVersionUID = -3409604860829442685L;
	
	 private String paySign;
	 private String prepay_id;
	 private String nonce_str;
	 private String timeStamp;
	 
	 
	 /**
	     * 获取页面上weixin支付JS所需的参数
	     * @param map
	     * @return
	     *
	     *	 **/
	    public static WxParams getWxPayInfo(String prepay_id) {
	  	  String nonce = MoneyUtils.buildRandom();
	    	//String  nonce= create_nonce_str();
    	 // String timeStamp = MoneyUtils.create_timestamp();
	    	String timeStamp = create_timestamp();
	        //再算签名
    	  String newPrepay_id = "prepay_id="+prepay_id;
    	  HashMap<String, Object> map = new HashMap<String, Object>();
    	  map.put("appId", CommonUtil.appId);
    	  map.put("nonceStr", nonce);
    	  map.put("package", prepay_id);
    	  map.put("signType", "MD5");
    	  map.put("timeStamp", timeStamp);
    	  String paySign = MoneyUtils.createSign(map);
    	  
    	  WxParams wxPay = new WxParams();
	        wxPay.setNonce_str(nonce);
	        wxPay.setPaySign(paySign);
	        wxPay.setPrepay_id(prepay_id);
	        wxPay.setTimeStamp(timeStamp);
	        return wxPay;
	    }
	    
	    @Override
	    public String toString() {
	    	StringBuilder s = new StringBuilder();
	    	s.append("paySign").append("=").append(paySign).append("&").append("prepay_id").append("=").append(prepay_id)
	    	.append("&").append("nonce_str=").append(nonce_str).append("&").append("timeStamp=").append(timeStamp);
	   
	    	return s.toString();
	    }

	   
	public String getPaySign() {
		return paySign;
	}
	public void setPaySign(String paySign) {
		this.paySign = paySign;
	}
	public String getPrepay_id() {
		return prepay_id;
	}
	public void setPrepay_id(String prepay_id) {
		this.prepay_id = prepay_id;
	}
	public String getNonce_str() {
		return nonce_str;
	}
	public void setNonce_str(String nonce_str) {
		this.nonce_str = nonce_str;
	}
	public String getTimeStamp() {
		return timeStamp;
	}
	public void setTimeStamp(String timeStamp) {
		this.timeStamp = timeStamp;
	}
	
	
	 private static String create_nonce_str() {  
	        return UUID.randomUUID().toString();  
	    }  
	    
	    private static String create_timestamp() {  
	        return Long.toString(System.currentTimeMillis() / 1000);  
	    }  
	    
	 
}